package com.kxf.androidtestdemo;

import android.app.Application;
import android.content.Context;
import android.os.Environment;
import android.util.Log;
import android.view.Choreographer;

import com.kxf.androidtestdemo.util.EpicHookUtils;
import com.kxf.logmanagerjava.LogManager;
import com.kxf.utils.ProcessUtil;
import com.kxf.webviewcache.WebViewCacheManager;
import com.squareup.leakcanary.LeakCanary;

import java.util.concurrent.TimeUnit;

public class MyApplication extends Application {
    // 下载文件保存目录
    public static final String SAVE_PATH = isExistSDCard() + "/SilentUpgrade/";


    private Choreographer.FrameCallback callback = new Choreographer.FrameCallback() {
        long lastFrameTimeNanos = 0;
        long currentFrameTimeNanos = 0;
        private int count = 0;

        @Override
        public void doFrame(long frameTimeNanos) {
            if (lastFrameTimeNanos == 0) {
                lastFrameTimeNanos = frameTimeNanos;
            }
            currentFrameTimeNanos = frameTimeNanos;
            long diffMs = TimeUnit.MILLISECONDS.convert(currentFrameTimeNanos - lastFrameTimeNanos, TimeUnit.NANOSECONDS);
            lastFrameTimeNanos = currentFrameTimeNanos;
            if(count>5){
                count = 0;
                Log.d("MyApplication", "diffMs=" + diffMs);
                int fps = (int) (1000 / diffMs);
                Log.d("MyApplication", "fps=" + fps);
            }else {
                count ++;
            }
            if (diffMs == 0) {
                diffMs = (long) 16.7;
            }
            //为Choreographer设置一个回调，当一帧开始渲染时触发。
            Choreographer.getInstance().postFrameCallback(callback);

//                if (isShowFPS) {
//                    long current = System.currentTimeMillis();
//                    if (current - mLastFPSRefreshTs > refreshInterval) {
//                        int fps = (int) (1000 / diffMs);
//                        refreshFPS(fps);
//                        mLastFPSRefreshTs = current;
//                    }
//                }
//
//                if (diffMs > 16.7f) {
//                    long droppedCount = (long) (diffMs / 16.7f);
//                    if (droppedCount > 1) {
//                        System.out.println("掉帧数 : " + droppedCount);
//                    }
//                }
//
//                if (UiBlockLogMonitor.getInstance().isMonitor()) {
//                    UiBlockLogMonitor.getInstance().stopMonitor();
//                }
//
//                if (isDetectContinue) {
//                    UiBlockLogMonitor.getInstance().startMonitor();
//                    Choreographer.getInstance().postFrameCallback(this);
//                }
        }
    };

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d("MyApplication", "onCreate isMainProcess=" + ProcessUtil.isMainProcess(this));
        if (LeakCanary.isInAnalyzerProcess(this)) {//1
            // This process is dedicated to LeakCanary for heap analysis.
            // You should not init your app in this process.
            return;
        }
        LeakCanary.install(this);

        //为Choreographer设置一个回调，当一帧开始渲染时触发。
//        Choreographer.getInstance().postFrameCallback(callback);


        isExistSDCard();

        LogManager.setConfig();

        Thread.setDefaultUncaughtExceptionHandler((t, e) -> {
            e.printStackTrace();
        });

        WebViewCacheManager.INSTANCE.prepare(this);

        EpicHookUtils.hook();
    }

    /**
     * 获取手机存储根目录<p>
     * 如果SD卡存在则获取SD卡根目录，不存在则获取data/data中应用的files目录
     *
     * @return 路径
     */
    public static String getRootFilePath(Context context) {
        if (isExistSDCard()) {
            return Environment.getExternalStorageDirectory().getAbsolutePath();
        } else {
            return context.getFilesDir().getAbsolutePath();
        }
    }

    private static boolean isExistSDCard() {
        Log.d("kxf", "Environment.getExternalStorageState()=" + Environment.getExternalStorageState());
        Log.d("kxf", "Environment.MEDIA_MOUNTED=" + Environment.MEDIA_MOUNTED);
        return Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED);
    }
}
